package pe.ejd.transformers;

import java.io.File;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class StringToXml extends AbstractTransformer{

	@Override
	protected Object doTransform(Object src, String enc)
			throws TransformerException {
		// TODO Auto-generated method stub
		System.out.println(src);
		System.out.println(enc);
		StringBuffer xmlString = new StringBuffer((String)src);
		xmlString = new StringBuffer(xmlString.toString().replace("ns0:", ""));
		xmlString = new StringBuffer(xmlString.toString().replace("xmlns:ns0", "xmlns"));
		
		//xmlString = new StringBuffer(xmlString.toString().replace("consultaExpedientePorNumero", "luis"));
		
		//xmlString = new StringBuffer(xmlString.toString().replace("luisResponse", "consultaExpedientePorNumeroResponse"));
		
		System.out.println(xmlString.toString());
		
		DocumentBuilderFactory fctr = null;
		DocumentBuilder bldr = null;
		InputSource insrc = null;
		Document document = null;
		try {
			fctr = DocumentBuilderFactory.newInstance();
			bldr = fctr.newDocumentBuilder();
			insrc = new InputSource(new StringReader(xmlString.toString()));	
			document = bldr.parse(insrc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*String uri = document.getElementsByTagName("consultaExpedientePorNumeroResponse").item(0).getTextContent();
		System.out.println(uri);*/
		return document;

	}

}
